package com.flower.controller.buyer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.annotation.Log;
import com.flower.domain.dto.buyer.shop.ShopDto;
import com.flower.domain.dto.buyer.shop.ShopQueryDto;
import com.flower.domain.dto.buyer.product.ProductQueryDto;
import com.flower.domain.vo.buyer.ProductVO;
import com.flower.domain.vo.buyer.ShopVO;
import com.flower.service.ShopService;
import com.flower.utils.AjaxResult;
import com.flower.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 购买者端店铺信息
 */
@Slf4j
@RestController("buyerShop")
@RequestMapping("/buyer/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    /**
     * 获取店铺列表
     */
    @GetMapping("/list")
    public AjaxResult getShopList(ShopQueryDto shopDto) {
        Page<ShopVO> shopList = shopService.pageList(shopDto);
        return AjaxResult.success(shopList);
    }

    /**
     * 获取店铺详情
     */
    @GetMapping("/{shopId}")
    public AjaxResult getShopDetail(@PathVariable Integer shopId) {
        ShopVO shop = shopService.getShopDetail(shopId);
        return AjaxResult.success(shop);
    }

    /**
     * 获取店铺商品列表
     */
    @GetMapping("/products")
    public AjaxResult getShopProducts(ProductQueryDto productQueryDto) {
        Page<ProductVO> products = shopService.getShopProducts(productQueryDto);
        return AjaxResult.success(products);
    }

    /**
     * 获取当前用户的店铺信息
     */
    @GetMapping("/my")
    public AjaxResult getMyShop() {
        Long buyerId = UserContext.getUserId();
        ShopVO shop = shopService.getShopByBuyerId(buyerId.intValue());
        return AjaxResult.success(shop);
    }

    /**
     * 开店
     */
    @PostMapping("/open")
    public AjaxResult openShop(@RequestBody ShopDto shopDto) {
        shopService.openShop(shopDto);
        return AjaxResult.success();
    }

    /**
     * 更新店铺信息
     */
    @PutMapping("/update")
    public AjaxResult updateShop(@RequestBody ShopDto shopDto) {
        shopService.updateShop(shopDto);
        return AjaxResult.success();
    }

    /**
     * 删除店铺
     */
    @DeleteMapping("/{shopId}")
    public AjaxResult deleteShop(@PathVariable Integer shopId) {
        Long buyerId = UserContext.getUserId();
        shopService.deleteShop(buyerId.intValue(), shopId);
        return AjaxResult.success();
    }

    /**
     * 更新店铺点击量
     */
    @PutMapping("/click/{shopId}")
    public AjaxResult updateClickCount(@PathVariable Integer shopId) {
        boolean result = shopService.updateClickCountById(shopId);
        return AjaxResult.success(result);
    }

    // 更新店铺状态
    @Log(name = "更新店铺状态")
    @PutMapping("/status")
    public AjaxResult updateShopStatus() {
        shopService.updateShopStatus();
        return AjaxResult.success();
    }

    /**
     * 获取店铺销售信息
     * @param timeRange 时间范围：today(今日)、week(本周)、month(本月)
     * @return 店铺概览数据
     */
    @GetMapping("/overview")
    public AjaxResult getShopOverview(@RequestParam(defaultValue = "today") String timeRange) {
        // 参数验证
        if (!timeRange.equals("today") && !timeRange.equals("week") && !timeRange.equals("month")) {
            return AjaxResult.error("无效的时间范围参数，有效值：today, week, month");
        }
        
        try {
            return AjaxResult.success(shopService.getShopOverview(timeRange));
        } catch (Exception e) {
            log.error("获取店铺概览数据失败", e);
            return AjaxResult.error(e.getMessage());
        }
    }
} 